package com.suncnpap.base.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.app.util.common.Constants;
import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.reader.model.Reader;
import com.suncnpap.system.model.Admin;
import com.suncnpap.system.model.GroupType;
import com.suncnpap.system.model.Menu;
import com.suncnpap.system.service.GroupTypeService;
import com.suncnpap.system.service.MenuService;

@Controller
public class GetUserController extends BaseController {

	@Autowired
	MenuService menuService;
	
	@Autowired
	GroupTypeService groupTypeService;
	
	@RequestMapping(value="/getUserInfo",method=RequestMethod.POST)
	public void getUserInfo(HttpServletRequest request,HttpServletResponse response) {
		
		HandlerResult result = this.checkToken(request);

		if (result.getCode()==0) {//
			Object[] object = GlobaUser.getUser(request.getHeader("token"));
			if(object!=null){
				if(object[0].equals("reader")){
					Reader reader = (Reader) object[1];
					result.put("roles", "reader");
					result.put("introduction", "");
					result.put("avatar", "");
					result.put("name", reader.getStrName());
					result.put("userId", reader.getStrCardNumber());
					result.put("schoolId", reader.getStrSchoolId());
					result.put("strCollectionLocationId", reader.getStrCollectionLocationId());
					result.put("strCollectionLocation", reader.getStrCollectionLocation());
					
					Page page = new Page();
					page.addQueryAnd("strNature", "user");
			    	List<Menu> menuList = menuService.getList(page, "");
			    	String range = ObjUtil.getFieldValues(menuList, "strId");
			    	
			    	Map<String, List<Menu>> menuMap = ObjUtil.listToMapByField(menuList, "strParentId");
			    	menuList = creatMenuTree(menuList, menuMap);
					List<Map<String,Object>> objList = new ArrayList<Map<String,Object>>();
					List<Map<String,Object>> childrenList = new ArrayList<Map<String,Object>>();
					
					if(menuList!=null && menuList.size()>0){
						for(Menu obj:menuList){
							if(obj.getStrParentId().equals("-1")){
								Map<String,Object> temp = new HashMap<String,Object>();
								temp.put("strParentNodeId", obj.getStrId());
								temp.put("strParentNodeName", obj.getStrName());
								List<Map<String,Object>> tempJson = creatJsonTree(obj.getChildren(),range,false);
								if(tempJson!=null && tempJson.size()>0){
									temp.put("children", tempJson);
								}
								temp.put("expand", false);
								if( (tempJson != null && tempJson.size()>0)){
									childrenList.add(temp);
								}
							}
						}
						
					}
					Map<String,Object> tempMap = new HashMap<String,Object>();
					tempMap.put("strParentNodeId", "-1");
					tempMap.put("strParentNodeName", "菜单列表");
					if(childrenList!=null && childrenList.size()>0){
						tempMap.put("children", childrenList);
					}
					tempMap.put("expand", true);
					objList.add(tempMap);
					
					result.put("objList", objList);
				}else{
					Admin admin = (Admin) object[1];
					result.put("roles", "admin");
					result.put("introduction", "");
					result.put("avatar", "");
					result.put("name", admin.getStrName());
					result.put("userId", admin.getStrUser());
					result.put("schoolId", admin.getStrSchoolId());
					result.put("strCollectionLocationId", admin.getStrCollectionLocationId());
					result.put("strCollectionLocation", admin.getStrCollectionLocation());
					Boolean flag = false;
					if(admin.getStrUser().equals(Constants.INIT_USER_ID)){
						flag = true;
					}
					
					String strGroupTypeId = admin.getStrGroupTypeId();
					if((strGroupTypeId!=null && !strGroupTypeId.equals(""))||flag){
						GroupType groupType = groupTypeService.findById(strGroupTypeId);
						String range = "";
						if(groupType!=null){
							range = groupType.getStrMenuRange();
						}
						
						Page page = new Page();
						if(flag){
							page.addQueryAnd("strNature", QueryType.markNOTEQ, "normal");
						}
				    	List<Menu> menuList = menuService.getList(page, "");
				    	
				    	Map<String, List<Menu>> menuMap = ObjUtil.listToMapByField(menuList, "strParentId");
				    	menuList = creatMenuTree(menuList, menuMap);
						List<Map<String,Object>> objList = new ArrayList<Map<String,Object>>();
						List<Map<String,Object>> childrenList = new ArrayList<Map<String,Object>>();
						
						if(menuList!=null && menuList.size()>0){
							for(Menu obj:menuList){
								if(obj.getStrParentId().equals("-1")){
									Map<String,Object> temp = new HashMap<String,Object>();
									temp.put("strParentNodeId", obj.getStrId());
									temp.put("strParentNodeName", obj.getStrName());
									List<Map<String,Object>> tempJson = creatJsonTree(obj.getChildren(),range,flag);
									if(tempJson!=null && tempJson.size()>0){
										temp.put("children", tempJson);
									}
									temp.put("expand", false);
									System.out.println((range.indexOf(obj.getStrId())>-1)+"==="+(tempJson != null && tempJson.size()>0));
									if(range.indexOf(obj.getStrId())>-1 || (tempJson != null && tempJson.size()>0) || flag){
										childrenList.add(temp);
									}
								}
							}
							
						}
						Map<String,Object> tempMap = new HashMap<String,Object>();
						tempMap.put("strParentNodeId", "-1");
						tempMap.put("strParentNodeName", "菜单列表");
						if(childrenList!=null && childrenList.size()>0){
							tempMap.put("children", childrenList);
						}
						tempMap.put("expand", true);
						objList.add(tempMap);
						
						result.put("objList", objList);
//						result.put("menuList", menuList);
					}
				}
			}else{
				result.setCode(1);
    			result.setMessage("登录超时");
			}
			
	    	
		}
	    
	    ResponseUtil.writeJson(result, response);
	}
	
	private List<Map<String,Object>> creatJsonTree(List<Menu> menuList, String range, Boolean flag){
		List<Map<String,Object>> tempList = new ArrayList<Map<String,Object>>();
		if(menuList != null){
			for(Menu obj: menuList){
				Map<String,Object> tempMap = new HashMap<String,Object>();
				
				tempMap.put("strParentNodeId", obj.getStrId());
				tempMap.put("strParentNodeName", obj.getStrName());
				List<Map<String,Object>> tempJson = creatJsonTree(obj.getChildren(), range, flag);
				if(tempJson!=null && tempJson.size()>0){
					tempMap.put("children", tempJson);
				}
				tempMap.put("expand", false);
				if(range.indexOf(obj.getStrId())>-1 || (tempJson != null && tempJson.size()>0) || flag){
					tempList.add(tempMap);
				}
			}
		}
		return tempList;
	}
	
	private List<Menu> creatMenuTree(List<Menu> menuList,Map<String, List<Menu>> menuListMap){
		List<Menu> list=null;
		for(int i=0;menuList!=null&&i<menuList.size();i++){
			Menu obj=menuList.get(i);
			list=list==null?new ArrayList<Menu>():list;
			obj.setChildren(creatMenuTree(menuListMap.get(obj.getStrId()),menuListMap));
				
			list.add(obj);
		}
		return list;
	}
}
